
var linkflag = 0;

var selectedFile = 0;

var videoName;
var videoTag;

var videourl = 0;

function uploadVideo(file, videoName, videoTag) {
    const formData = new FormData();
    formData.append("video", file);
    formData.append("videoName", videoName);
    formData.append("videoTag", videoTag);

    fetch("http://127.0.0.1:8090/upload", {
        method: "POST",
        body: formData
    })
    .then(response => {
        if (!response.ok) {
            throw new Error("Network response was not ok");
        }
        return response.json();
    })
    .then(data => {
        console.log(data);
        // Handle response from server if needed
    })
    .catch(error => {
        console.error("There was a problem with the upload:", error);
    });
}


document.addEventListener("DOMContentLoaded",function(){

    

    var videoContainer = document.getElementById('videoContainer');

        
    var nameInput = document.getElementById('name');
    var tagInput = document.getElementById('tag');
  

    document.getElementById("name").addEventListener("change",function(event){

        videoContainer.innerHTML = ''
        videoName = nameInput.value;
        videoTag = tagInput.value;

        var nametext = document.createElement('div');
        nametext.className = "fab fa-youtube"

        nametext.innerHTML = "<span style='font-weight: bold;'> 视频名称 : </span> <span style='font-weight: bold;'>" + videoName + "</span>";

        nametext.style.margin = "20px"
        var tagtext = document.createElement('div');
        tagtext.style.margin = "20px"
        tagtext.className = "fa fa-tag"

        
        tagtext.innerHTML = "<span style='font-weight: bold;'> 视频标签 : </span> <span style='font-weight: bold;'>" + videoTag + "</span>";
     
        if(videoName != ''){
            videoContainer.appendChild(nametext)
        }
        
        if(videoTag != ''){
            videoContainer.appendChild(tagtext)
        }  

    })

    document.getElementById("tag").addEventListener("change",function(event){

        videoContainer.innerHTML = ''

        videoName = nameInput.value;
        videoTag = tagInput.value;

        var nametext = document.createElement('div');
        nametext.className = "fab fa-youtube"

        nametext.innerHTML = "<span style='font-weight: bold;'> 视频名称 : </span> <span style='font-weight: bold;'>" + videoName + "</span>";

        nametext.style.margin = "20px"
        
        var tagtext = document.createElement('div');
        tagtext.className = "fa fa-tag"
        tagtext.style.margin = "20px"

        tagtext.innerHTML = "<span style='font-weight: bold;'> 视频标签 : </span> <span style='font-weight: bold;'>" + videoTag + "</span>";
     
        if(videoName != ''){
            videoContainer.appendChild(nametext)
        }
        
        if(videoTag != ''){
            videoContainer.appendChild(tagtext)
        }
        

    })


    document.getElementById("video").addEventListener("change",function(event){

        selectedFile = event.target.files[0];

        

        const objectURL = URL.createObjectURL(selectedFile);

        const videoLink = document.createElement('a');
        videoLink.href = objectURL;
        videoLink.innerText = '点击查看视频';
        
        // 创建要添加的消息

        var videoContainers = document.getElementById('videoContainersrc');
        videoContainers.appendChild(videoLink)

        videourl = objectURL;

        // console.log("视频url" + videourl)

        linkflag = 1


    })

    document.getElementById('Close').addEventListener("click",function(){
        // 如果找到了 iframe 元素，则将其从父元素中移除
        let uploadhtml= window.parent.document.getElementById('uploadhtml');

        if (uploadhtml) {
            uploadhtml.parentNode.removeChild(uploadhtml);
        }
    })

    document.getElementById('summarizeButton').addEventListener("click",function(){

        if(linkflag == 1){

            let uploadhtml= window.parent.document.getElementById('uploadhtml');
            

            // 跳转到 player.html 页面，并传递视频链接参数

    
            var mainhtml = window.parent.document.getElementById("mainhtml");

            setVideoAnalysisStatus(false);
            uploadVideo(selectedFile, videoName, videoTag);
            mainhtml.src = "../static/html/player.html?videoSrc=" + encodeURIComponent(videourl);
    
            if (uploadhtml) {
                uploadhtml.style.display = 'none'
                uploadhtml.style.zIndex = "-9999"
            }

        }else if(linkflag == 0){
            
            window.alert("请先选择视频!")
        }

    
    })

})


// 设置视频成功分析完成的状态
function setVideoAnalysisStatus(completed) {
    // 将标志存储在localStorage中
    localStorage.setItem('videoAnalysisCompleted', completed.toString());
}